package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.userInfo.listener.OnMsgFunctionClickListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/7/9.
 * description：
 */
public abstract class MessageListActivity<P extends BasePresenter> extends BaseActivity<P> implements OnMsgFunctionClickListener {
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.txt_right)
    TextView txt_right;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.view_stub_empty)
    ViewStub viewStub;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    protected View inflate;
    protected RelativeLayout layoutEmpty;
    protected int pageIndex = 1;
    protected int pageSize = 10;
    //manager
    protected LinearLayoutManager manager = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_message_list;
    }

    protected int messageNum;//未读消息数

    @Override
    protected void initView() {
        Intent intent = getIntent();
        messageNum = intent.getIntExtra(Constant.INTENT_DATA, 0);
        if (messageNum > 0) {
            txt_right.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                pageIndex = 1;
                refreshLayout.resetNoMoreData();
                requestMessage();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                requestMessage();
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.head_left, R.id.head_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_left:
                finish();
                break;
            case R.id.head_right:
                txt_right.setVisibility(View.GONE);
                setAllRead();
                break;
        }
    }

    //网络请求数据
    protected abstract void requestMessage();

    //设置全部已读
    protected abstract void setAllRead();

    /**
     * 显示缺省页面
     */
    protected void showEmptyLayout() {
        if (inflate == null) {
            inflate = viewStub.inflate();
            layoutEmpty = inflate.findViewById(R.id.layout_empty);
            ImageView noDataImg = inflate.findViewById(R.id.no_data_img);
            TextView noDataHint = inflate.findViewById(R.id.no_data_hint);
            noDataImg.setImageResource(R.drawable.empty_message_bg);
            noDataHint.setText("信箱空空如也~");
        }
        layoutEmpty.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏缺省页面
     */
    protected void hideEmptyLayout() {
        if (layoutEmpty != null) {
            layoutEmpty.setVisibility(View.GONE);
        }
    }
}
